home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / matrx042.zip / MATCREAT.C < prev    next >
C/C++ Source or Header  |  1993-09-23  |  4KB  |  199 lines

  1. /*
  2. *-----------------------------------------------------------------------------
  3. *    file:    matcreat.c
  4. *    desc:    matrix mathematics - object creation
  5. *    by:    ko shu pui, patrick
  6. *    date:    24 nov 91 v0.1
  7. *    revi:    14 may 92 v0.2
  8. *        21 may 92 v0.3
  9. *    ref:
  10. *       [1] Mary L.Boas, "Mathematical Methods in the Physical Sciene,"
  11. *    John Wiley & Sons, 2nd Ed., 1983. Chap 3.
  12. *
  13. *    [2] Kendall E.Atkinson, "An Introduction to Numberical Analysis,"
  14. *    John Wiley & Sons, 1978.
  15. *
  16. *-----------------------------------------------------------------------------
  17. */
  18. #include <stdio.h>
  19.  
  20. #ifdef    __TURBOC__
  21. #include <alloc.h>
  22. #else
  23. #include <malloc.h>
  24. #endif
  25.  
  26. #include "matrix.h"
  27.  
  28. MATRIX    _mat_creat( row, col )
  29. int row, col;
  30. {
  31.     MATBODY    *mat;
  32.     int     i, j;
  33.  
  34.     if ((mat = (MATBODY *)malloc( sizeof(MATHEAD) + sizeof(double *) * row)) == NULL)
  35.         return (mat_error( MAT_MALLOC ));
  36.  
  37.     for (i=0; i<row; i++)
  38.     {
  39.     if ((*((double **)(&mat->matrix) + i) = (double *)malloc(sizeof(double) * col)) == NULL)
  40.         return (mat_error( MAT_MALLOC ));
  41.     }
  42.  
  43.     mat->head.row = row;
  44.     mat->head.col = col;
  45.  
  46.     return (&(mat->matrix));
  47. }
  48.  
  49. /*
  50. *-----------------------------------------------------------------------------
  51. *    funct:    mat_creat
  52. *    desct:    create a matrix
  53. *    given:  row, col = dimension, type = which kind of matrix
  54. *    retrn:    allocated matrix (use mat_free() to free memory)
  55. *-----------------------------------------------------------------------------
  56. */
  57. MATRIX    mat_creat( row, col, type )
  58. int row, col, type;
  59. {
  60.     MATRIX    A;
  61.  
  62.     if ((A =_mat_creat( row, col )) != NULL)
  63.         {
  64.         return (mat_fill(A, type));
  65.         }
  66.     else
  67.         return (NULL);
  68. }
  69.  
  70. /*
  71. *-----------------------------------------------------------------------------
  72. *    funct:    mat_fill
  73. *    desct:    form a special matrix
  74. *    given:  A = matrix, type = which kind of matrix
  75. *    retrn:    A
  76. *-----------------------------------------------------------------------------
  77. */
  78. MATRIX mat_fill( A, type )
  79. MATRIX A;
  80. int type;
  81. {
  82.     int    i, j;
  83.  
  84.     switch (type)
  85.         {
  86.         case UNDEFINED:
  87.             break;
  88.         case ZERO_MATRIX:
  89.         case UNIT_MATRIX:
  90.             for (i=0; i<MatRow(A); i++)
  91.             for (j=0; j<MatCol(A); j++)
  92.                 {
  93.                 if (type == UNIT_MATRIX)
  94.                     {
  95.                     if (i==j)
  96.                         {
  97.                         A[i][j] = 1.0;
  98.                         continue;
  99.                         }
  100.                     }
  101.                 A[i][j] = 0.0;
  102.                 }
  103.             break;
  104.         }
  105.     return (A);
  106. }
  107.  
  108.  
  109. /*
  110. *-----------------------------------------------------------------------------
  111. *    funct:    mat_free
  112. *    desct:    free an allocated matrix
  113. *    given:  A = matrix
  114. *    retrn:    nothing <actually 0 = NULL A passed, 1 = normal exit>
  115. *-----------------------------------------------------------------------------
  116. */
  117. int mat_free( A )
  118. MATRIX A;
  119. {
  120.     int i;
  121.  
  122.     if (A == NULL)
  123.         return (0);
  124.     for (i=0; i<MatRow(A); i++)
  125.         {
  126.         free( A[i] );
  127.         }
  128.     free( Mathead(A) );
  129.     return (1);
  130. }
  131.  
  132. /*
  133. *-----------------------------------------------------------------------------
  134. *    funct:    mat_copy
  135. *    desct:    duplicate a matrix
  136. *    given:    A = matrice to duplicated
  137. *    retrn:    C = A
  138. *    comen:
  139. *-----------------------------------------------------------------------------
  140. */
  141. MATRIX mat_copy( A )
  142. MATRIX A;
  143. {
  144.     int    i, j;
  145.     MATRIX    C;
  146.  
  147.     if ((C = mat_creat( MatRow(A), MatCol(A), UNDEFINED )) == NULL)
  148.         return (NULL);
  149.  
  150.     for (i=0; i<MatRow(A); i++)
  151.     for (j=0; j<MatCol(A); j++)
  152.         {
  153.         C[i][j] = A[i][j];
  154.         }
  155.     return (C);
  156. }
  157.  
  158.  
  159. MATRIX mat_colcopy1( A, B, cola, colb )
  160. MATRIX A, B;
  161. int cola, colb;
  162. {
  163.     int    i, n;
  164.  
  165.     n = MatRow(A);
  166.     for (i=0; i<n; i++)
  167.         {
  168.         A[i][cola] = B[i][colb];
  169.         }
  170.     return (A);
  171. }
  172.  
  173. int fgetmat( A, fp )
  174. MATRIX A;
  175. FILE *fp;
  176. {
  177.     int     i, j, k=0;
  178.  
  179.     for (i=0; i<MatRow(A); i++)
  180.     for (j=0; j<MatCol(A); j++)
  181.         {
  182. /*
  183. *    to avoid a bug in TC
  184. */
  185. #ifdef    __TURBOC__
  186.         {
  187.         double    temp;
  188.         k += fscanf( fp, "%lf", &temp );
  189.         A[i][j] = temp;
  190.         }
  191. #else
  192.         k += fscanf( fp, "%lf", &A[i][j] );
  193. #endif
  194.  
  195.         }
  196.  
  197.     return (k);
  198. }
  199.